﻿using System;

class PrintMatrixD
{
    static void Main()
    {
        //int[,] array = 
        //{
        //    { 1, 5, 9, 13 },
        //    { 2, 6, 10, 14 },
        //    { 3, 7, 11, 15 },
        //    { 1, 5, 9, 13 },
        //    { 4, 8, 12, 16 },
        //};
        int n = 6;
        int[,] array = new int[n, n];
        Fill(array, 0, 0, 1, 0);
    }  

    static void Fill(int[,] array, int indexX, int indexY, int counter, int direction)
    {
        if (counter > (array.GetLength(0) * array.GetLength(1)))
        {
            Print(array);
            return;
        }

        if (direction == 0)
        {
            array[indexX, indexY] = counter;
            counter++;

            if (isInRange(array, indexX + 1, indexY))
            {
                indexX++;
                Fill(array, indexX, indexY, counter, direction);
            }
            else
            {
                indexY++;
                Fill(array, indexX, indexY, counter, direction + 1);
            }
        }

        if (direction == 1)
        {
            array[indexX, indexY] = counter;
            counter++;

            if (isInRange(array, indexX, indexY + 1))
            {
                indexY++;
                Fill(array, indexX, indexY, counter, direction);
            }
            else
            {
                indexX--;
                Fill(array, indexX, indexY, counter, direction + 1);
            }
        }

        if (direction == 2)
        {
            array[indexX, indexY] = counter;
            counter++;

            if (isInRange(array, indexX - 1, indexY))
            {
                indexX--;
                Fill(array, indexX, indexY, counter, direction);
            }
            else
            {
                indexY--;
                Fill(array, indexX, indexY, counter, direction + 1);
            }
        }

        if (direction == 3)
        {
            array[indexX, indexY] = counter;
            counter++;

            if (isInRange(array, indexX, indexY - 1))
            {
                indexY--;
                Fill(array, indexX, indexY, counter, direction);
            }
            else
            {
                indexX++;
                Fill(array, indexX, indexY, counter, 0);
            }
        }
    }
        
    static void Print(int[,] array)
    {
        Console.WriteLine();
        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                Console.Write(" " + array[i, j]);
            }
            Console.WriteLine();
        }
    }

    static bool isInRange(int[,] array, int indexX, int indexY)
    {
        if (indexX < array.GetLength(0) && indexX >= 0 && indexY < array.GetLength(1) && indexY >= 0 && array[indexX, indexY] == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}